﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections;

namespace T15OOP2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Product p = new Product();//错误：抽象类不能实例化
                Book book = new Book();//具有默认或无参构造函数能正确初始化，无需显示调用
                book.Name = "天龙八部";
                book.Price = 30.0M;
                book.Isbn = "9343431111431";
                book.Intro = "江湖传奇";
                book.Show();
                book.ShowDetail();
                //book = null;//谨慎使用，可以提高内容使用效率
                Console.WriteLine("==========================");
                Digital digital = new Digital();
                digital.Name = "蓝牙音箱";
                digital.Price = 399M;
                digital.Barcode = "1120303403";

                Action buy = new Action();
                buy.Buy(book);
                buy.Buy(digital);

                buy.Show();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("输入信息有误!");
            }
            catch (Exception e)
            {
                Console.WriteLine("其他错误");
            }

            //内存垃圾回收
            System.GC.Collect();

            Console.ReadKey();
        }
    }
}



 

namespace T15OOP2
{
    abstract class Product
    {
        protected string name;//产品名
        protected decimal price;//价格
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public decimal Price
        {
            get { return price; }
            set { price = value; }
        }
        //this("") 表示调用带参的构造函数
        public Product() : this("")
        {
            Debug.WriteLine("-=调用Product无参构造方法=-");
        }

        public Product(string name)
        {
            this.name = name;
            Debug.WriteLine("-=调用Product带参构造方法=-");
        }

        public virtual void Show()
        {
            Console.WriteLine("产品名称:{0}", name);
            Console.WriteLine("产品价格:{0}", price);
        }

        /// <summary>
        /// 显示产品详细信息
        /// </summary>
        public abstract void ShowDetail();

        ~Product()
        {
            Debug.WriteLine("-=调用Product析构方法=-");
        }
    }
}



namespace T15OOP2
{
    class Book : Product
    {
        private string isbn;//书号
        private string intro;//简介

        public string Isbn
        {
            get { return isbn; }
            set
            {
                //异常处理，抛出异常
                if (value.Length != 13)
                {
                    throw new ArgumentException("请输入13位的ISBN号");
                }
                isbn = value;
            }
        }
        public string Intro { get { return intro; } set { intro = value; } }



        //base 表示调用基类指定构造方法（显示调用）
        public Book() : base("无名书籍")
        {
            Debug.WriteLine("-=调用Book默认构造方法=-");
        }

        public override void ShowDetail()
        {
            Console.WriteLine("ISBN:{0}", isbn);
            Console.WriteLine("简介:{0}", intro);
        }

        public override void Show()
        {
            //base.Show();//显示调用基类被覆盖的方法
            Console.WriteLine("书籍名称:{0}", name);
            Console.WriteLine("书籍价格:{0}", price);
        }

        ~Book()
        {
            Debug.WriteLine("-=调用Book析构方法=-");
        }


    }
}



namespace T15OOP2
{
    class Digital : Product
    {
        private string barcode;//条码
        public string Barcode { get => barcode; set => barcode = value; }

        public override void ShowDetail()
        {
            Console.WriteLine("条码:{0}", barcode);
        }
    }
}



namespace T15OOP2
{
    interface ICart
    {
        bool Add(Product p);
        bool Remove(int index);
        bool Update(int index);
        object Find(int index);
        void show();
    }
}






namespace T15OOP2
{
    class Action : ICart
    {
        private ArrayList products = new ArrayList();

        public void Buy(Product p)
        {
            Console.WriteLine("购买{0}", p.Name);
            Add(p);
        }

        public bool Add(Product p)
        {
            products.Add(p);
            return true;
        }

        public void Show()
        {
            foreach (Product p in products)
            {
                if (p is Book)//调用下转型对象(book)独有的方法
                {
                    Console.WriteLine((p as Book).Isbn);
                }
                p.Show();//运行时多态
                p.ShowDetail();
            }
        }

        public object Find(int index)
        {
            throw new NotImplementedException();
        }

        public bool Remove(int index)
        {
            throw new NotImplementedException();
        }

        public bool Update(int index)
        {
            throw new NotImplementedException();
        }

        public void show()
        {
            throw new NotImplementedException();
        }
    }
}
